package com.atguigu.gulimall.order.config;

import com.atguigu.common.constant.SeckillConstant;
import com.atguigu.gulimall.order.entity.OrderEntity;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.EnableRabbit;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@EnableRabbit
@Configuration
@Slf4j
public class OrderRabbitMQConfig {

    public static final String ORDER_CREATE_ROUTE_KEY_NAME = "order.create.order";
    public static final String ORDER_RELEASE_ROUTE_KEY_NAME = "order.release.order";
    public static final String ORDER_RELEASE_OTHER_ROUTE_KEY_NAME = "order.release.other";
    public static final String ORDER_EVENT_EXCHANGE_NAME = "order-event-exchange";
    public static final String ORDER_DELAY_QUEUE_NAME = "order.delay.order.queue";
    public static final String ORDER_RELEASE_QUEUE_NAME = "order.release.order.queue";

    public static final String WARE_RELEASE_QUEUE_NAME = "ware.release.ware.queue";
    public static final Long ORDER_DELAY_QUEUE_TTL = 90_000L;

//    @RabbitListener(queues = ORDER_RELEASE_QUEUE_NAME)
//    public void cancelOrder(OrderEntity order, Message message, Channel channel) {
//        log.info("receive message {}, {}, {}", order, message, channel);
//    }

    @Bean
    public Queue orderDelayQueue() {
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-dead-letter-exchange", ORDER_EVENT_EXCHANGE_NAME);
        args.put("x-dead-letter-routing-key", ORDER_RELEASE_ROUTE_KEY_NAME);
        args.put("x-message-ttl", ORDER_DELAY_QUEUE_TTL);
        return new Queue(ORDER_DELAY_QUEUE_NAME, true, false, false, args);
    }

    @Bean
    public Queue orderReleaseQueue() {
        return new Queue(ORDER_RELEASE_QUEUE_NAME, true, false, false, null);
    }

    @Bean
    public Binding orderDelayBinding() {
        return new Binding(ORDER_DELAY_QUEUE_NAME, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE_NAME, ORDER_CREATE_ROUTE_KEY_NAME, null);
    }

    @Bean
    public Binding orderReleaseOrderBinding() {
        return new Binding(ORDER_RELEASE_QUEUE_NAME, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE_NAME, ORDER_RELEASE_ROUTE_KEY_NAME, null);
    }

    @Bean
    public Binding orderReleaseOtherBinding() {
        return new Binding(WARE_RELEASE_QUEUE_NAME, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE_NAME, ORDER_RELEASE_OTHER_ROUTE_KEY_NAME, null);
    }

    @Bean
    public Exchange orderEventExchange() {
        return new TopicExchange(ORDER_EVENT_EXCHANGE_NAME, true, false);
    }


    @Bean
    public Queue seckillOrderQueue() {
        return new Queue(SeckillConstant.SECKILL_ORDER_QUEUE_NAME, true, false, false, null);
    }

    @Bean
    public Binding seckillOrderBinding() {
        return new Binding(SeckillConstant.SECKILL_ORDER_QUEUE_NAME, Binding.DestinationType.QUEUE, ORDER_EVENT_EXCHANGE_NAME, SeckillConstant.SECKILL_ORDER_ROUTE_KEY_NAME, null);
    }

    @Bean
    public Jackson2JsonMessageConverter jsonMessageConverter() {
        Jackson2JsonMessageConverter jsonConverter = new Jackson2JsonMessageConverter();
        return jsonConverter;
    }
}
